package com.itheima.yichang;

/**
 * @author zhangbaiyan
 * @date 2024-09-18 13:31
 * @description:
 * @version:
 */
public class Girlfriend {
    private String name;
    private int age;

    public Girlfriend() {
    }

    public Girlfriend(String name, int age) {
        this.name = name;
        this.age = age;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     * 运行时期异常 RuntimeException 可以不写 因为是运行时期异常
     */
    public void setName(String name) throws RuntimeException {
        int len = name.length();
        if(len<2 || len>6){
            throw new NameFormatException(name+"名字长度必须在2~6之间");
        }
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        if(age<18 || age>30){
            throw new AgeOutOfBoundsException(age+"年龄必须在18~30之间");
        }
        this.age = age;
    }

    public String toString() {
        return "Girlfriend{name = " + name + ", age = " + age + "}";
    }
}
